package com.lc.springboot.service.impl;

import com.lc.springboot.mapper.SysMenuMapper;
import com.lc.springboot.mapper.SysUserMapper;
import com.lc.springboot.model.SysUser;
import com.lc.springboot.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Set;

/**
 * @author lengchuan <lishuijun1992@gmail.com>
 * @date 16-8-30
 */
@Service
@Transactional
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    public SysUser getUser(Integer id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    public int addUser(SysUser sysUser) {
        SysUser tmp = sysUserMapper.selectUserByName(sysUser.getLoginName());
        if (tmp != null) {
            return 0;
        }
        return sysUserMapper.insert(sysUser);
    }

    public SysUser getUserByName(String name) {
        return sysUserMapper.selectUserByName(name);
    }

    public Set<String> findUserMenus(String name) {

        return new HashSet<String>(sysUserMapper.getUserMenus(sysUserMapper.selectUserByName(name).getId()));
    }

    public Set<String> findUserRoles(String name) {
        return new HashSet<String>(sysUserMapper.getUserRoles(sysUserMapper.selectUserByName(name).getId()));
    }
}
